#include "apue.h"
#include <sys/stat.h>
#include <time.h>

void file_type_print(int argc, char const *argv[]);
void file_last_time_info_print(struct stat *sb);

int main(int argc, char const *argv[])
{
    file_type_print(argc, argv);
    return 0;
}

void file_type_print(int argc, char const *argv[])
{
    int i;
    struct stat buf;
    char *ptr;

    for (i = 1; i < argc; i++)
    {
        printf("%s: ", argv[i]);

        if (lstat(argv[i], &buf) < 0)
        {
            err_ret("lstat error");
            continue;
        }

        if (S_ISREG(buf.st_mode))
        {
            ptr = "regular";
        }
        else if (S_ISDIR(buf.st_mode))
        {
            ptr = "directory";
        }
        else if (S_ISCHR(buf.st_mode))
        {
            ptr = "character device";
        }
        else if (S_ISBLK(buf.st_mode))
        {
            ptr = "block device";
        }
        else if (S_ISFIFO(buf.st_mode))
        {
            ptr = "FIFO/pipe";
        }
        else if (S_ISLNK(buf.st_mode))
        {
            ptr = "symlink";
        }
        else if (S_ISSOCK(buf.st_mode))
        {
            ptr = "socket";
        }
        else
        {
            ptr = "unknown?";
        }
        
        printf("%s\n", ptr);
        file_last_time_info_print(&buf);
    }
}

void file_last_time_info_print(struct stat *sb)
{
    printf("Last status change:       %s", ctime(&sb->st_ctime));
    printf("Last file access:         %s", ctime(&sb->st_atime));
    printf("Last file modification:   %s", ctime(&sb->st_mtime));
}
